//
//  HotSellerMoreCollectionViewController.m
//  GiftItems
//
//  Created by lanou3g on 15/9/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "HotSellerMoreCollectionViewController.h"
#import "HotSellerMoreCollectionViewCell.h"
#import "BlurImageView.h"
#import "HotSellerCollectionReusableView.h"
#import "HotSellerMoreModel.h"
#import "HotSelListCollectionViewController.h"
@interface HotSellerMoreCollectionViewController ()
@property(nonatomic, strong) NSMutableArray *titleArray;
@property(nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation HotSellerMoreCollectionViewController

static NSString * const reuseIdentifier = @"Cell";
// 数据数组懒加载
- (NSMutableArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [[NSMutableArray alloc] init];
    }
    return _titleArray;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


// 数据解析
- (void)setUpDataWithUrl {
    
    [LORequestManger GET:KMoreGiftUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *Dic in dic[@"data"][@"categories"]) {
            HotSellerMoreModel *model = [HotSellerMoreModel shareJsonWithDictionary:Dic];
            [self.titleArray addObject:model];
            NSArray *array = Dic[@"subcategories"];
            NSMutableArray *dataArray = [[NSMutableArray alloc] init];
            for (NSDictionary *dictionary in array) {
               // NSLog(@"<><><>%@",dictionary.allKeys);
                HotSellerMoreModel *hotModel = [HotSellerMoreModel shareJsonWithDictionary:dictionary];
                [dataArray addObject:hotModel];
            }
            [self.dataArray addObject:dataArray];
        }
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpDataWithUrl];
    BlurImageView *blurView = [[BlurImageView alloc] initWithFrame:kBounds];
    self.collectionView.backgroundView = blurView;
    [self.collectionView registerClass:[HotSellerMoreCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerClass:[HotSellerCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"view"];
    // 设置返回的按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = leftButton;
     
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
     return self.titleArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.dataArray[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotSellerMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    HotSellerMoreModel *hotModel = self.dataArray[indexPath.section][indexPath.row];
    [cell setValueOfAttributesWithModel:hotModel];
    return cell;
}

#pragma mark <UICollectionViewDelegate>

// cell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HotSellerMoreModel *hotModel = self.dataArray[indexPath.section][indexPath.row];
    // 拼接下一个界面的网址
    NSString *listUrl = [KMoreGiftDetailUrl stringByReplacingOccurrencesOfString:@"参数" withString:[NSString stringWithFormat:@"%@",hotModel.ID]];
    // 设置下一个界面的布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [self setLayoutForCollectionView:layout];
    HotSelListCollectionViewController *hotListVC = [[HotSelListCollectionViewController alloc] initWithCollectionViewLayout:layout];
    hotListVC.listUrl = listUrl;
    hotListVC.vcName = hotModel.name;
    [self.navigationController pushViewController:hotListVC animated:YES];
    
}
// 设置下一界面布局的样式
- (void)setLayoutForCollectionView:(UICollectionViewFlowLayout *)layout {
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(5, 20, 5, 20);
    layout.itemSize = CGSizeMake((kWidth - 55)/2, 200);
}

// 增广视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    HotSellerCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"view" forIndexPath:indexPath];
    HotSellerMoreModel *model = self.titleArray[indexPath.section];
    [headView setValueOfAttributesWithModel:model];
    return headView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
